package cn.iocoder.yudao.module.diy.controller.admin.config;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.controller.admin.config.param.*;
import cn.iocoder.yudao.module.diy.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.diy.enums.ConfigKeyEnum;
import cn.iocoder.yudao.module.diy.enums.WechatEncryptionTypeEnum;
import cn.iocoder.yudao.module.diy.pojo.SceneDomainVo;
import cn.iocoder.yudao.module.diy.service.config.DiyConfigService;
import cn.iocoder.yudao.module.diy.utils.RequestUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.net.InetAddress;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.successAdmin;

@Tag(name = "管理后台 - 渠道管理")
@RestController
@RequestMapping("/diy/channel")
@Validated
public class ChannelConfigController {

    @Resource
    private DiyConfigService configService;

    /**
     * 获取pc端配置
     * @return Result<H5ConfigVo>
     */
    @GetMapping("/pc/config")
    public CommonResult<PcConfigVo> getPc() {
        JSONObject pcJson=configService.getConfigValue("pc");
        PcConfigVo vo = new PcConfigVo();
        if(ObjectUtil.isNotNull(pcJson))
        {
            vo = JSONUtil.toBean(pcJson.toString(), PcConfigVo.class);
        }
        return successAdmin(vo);
    }

    /**
     * 设置pc端配置
     * @return Result
     */
    @PutMapping("/pc/config")
    public CommonResult<Object> setPc(@Validated @RequestBody SetPcParam param) {
        if (null==param.getSiteId()) {
            param.setSiteId(TenantContextHolder.getTenantId());
        }
        configService.setConfig(TenantContextHolder.getTenantId(),0l,param.getSiteId(),"pc", JSONUtil.parseObj(param));
        return successAdmin(true);
    }

    /**
     * 获取pc端配置
     * @return Result<H5ConfigVo>
     */
    @GetMapping("/h5/config")
    public CommonResult<PcConfigVo> getH5() {
        JSONObject pcJson=configService.getConfigValue("h5");
        PcConfigVo vo = new PcConfigVo();
        if(ObjectUtil.isNotNull(pcJson))
        {
            vo = JSONUtil.toBean(pcJson.toString(), PcConfigVo.class);
        }
        return successAdmin(vo);
    }

    /**
     * 设置pc端配置
     * @return Result
     */
    @PutMapping("/h5/config")
    public CommonResult<Object> setH5(@Validated @RequestBody SetH5Param param) {
        if (null==param.getSiteId()) {
            param.setSiteId(TenantContextHolder.getTenantId());
        }
        configService.setConfig( TenantContextHolder.getTenantId(),0l,param.getSiteId(),"h5",  JSONUtil.parseObj(param));
        return successAdmin(true);
    }

}